<?php
// +----------------------------------------------------------------------
// | xiongrui [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.xiongrui.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.xiongrui.cn>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\common\controller\Admin;

class Goodscat extends Admin {
  protected $Goodscat;
  public function _initialize() {
		parent::_initialize();
    $this->Goodscat = model('Goodscat');
	}

  public function index() {
		$map  = array('status' => array('gt', -1));
		$list = $this->Goodscat->where($map)->order('sort asc,id desc')->column('*', 'id');
    	Cookie('__forward__',$_SERVER['REQUEST_URI']);
    //  model('goods')->where('cat_sort', 0)->update(['cat_sort'=>  100]);
		$this->assign('list', $list);
		$this->setMeta('商品分类');
		return $this->fetch();
	}

	public function add() {
    if (IS_POST) {
      	$result = $this->Goodscat->change();
      if($result){
        model('goods')->where('cat_id', $result['id'])->update(['cat_sort'=>  $result['sort']]);
        $this->success('添加成功！',  url('admin/Goodscat/index'));
      }else{
        $this->error('添加失败！');
      }
    } else {
			$data = array(
        'info'    => array('status' => 1,'sort'=>1),
				'keyList' => $this->Goodscat->addfield(),
			);
			$this->assign($data);
			$this->setMeta("添加分类");
			return $this->fetch('public/edit');
		}
  }

  public function edit($id) {
		if (!$id) {
			return $this->error("非法操作！");
		}
		if (IS_POST) {
			$result = $this->Goodscat->change();
			if ($result) {

        model('goods')->where('cat_id', $result['id'])->update(['cat_sort'=>  $result['sort']]);

				return $this->success("编辑成功！", url('admin/Goodscat/index'));
			} else {
				return $this->error("编辑失败！");
			}
		} else {
			$info = $this->Goodscat->where(array('id' => $id))->find();
			$data = array(
				'info'    => $info,
				'keyList' => $this->Goodscat->addfield(),
			);
			$this->assign($data);
			$this->setMeta('编辑分类');
			return $this->fetch('public/edit');
		}
	}

  public function updateGoodsCatsort($cid, $sort = 1){

  }

  public function del() {
		$id = $this->getArrayParam('id');
		if (empty($id)) {
			return $this->error("非法操作！");
		}
		$result = $this->Goodscat->where(array('id' => array('IN', $id)))->delete();
		if ($result) {
			return $this->success("删除成功！");
		} else {
			return $this->error("删除失败！");
		}
	}



  public function sort() {
		if (IS_GET) {
			$ids = input('ids');
			//获取排序的数据
			$map = array('status' => array('gt', -1));
			if (!empty($ids)) {
				$map['id'] = array('in', $ids);
			} else {
			}
			$list = db('Goodscat')->where($map)->field('id,cat_name')->order('sort asc,id asc')->select();

			$this->assign('list', $list);
			$this->setMeta('排序');
			return $this->fetch();
		} elseif (IS_POST) {
			$ids = input('post.ids');
			$ids = explode(',', $ids);
			foreach ($ids as $key => $value) {
				$res = db('Goodscat')->where(array('id' => $value))->setField('sort', $key + 1);
			}
			if ($res !== false) {
				return $this->success('排序成功！', url('admin/Goodscat/index'));
			} else {
				return $this->error('排序失败！');
			}
		} else {
			return $this->error('非法请求！');
		}
	}

}

 ?>
